﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;

namespace Contrast
{
    public static class IOHelp
    {
        static System.IO.Ports.SerialPort Port = new System.IO.Ports.SerialPort();

        public static string wechatId = "";

        public static int counts = 0;
        public static int AlertTime = 1000;
        public static bool IsAlert = false;


        /// <summary>
        /// 串口
        /// </summary>
        /// <param name="com"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static string Open(string com, string baudRate)
        {


            try
            {
                if (!Port.IsOpen)
                {

                    Port.BaudRate = int.Parse(baudRate);

                    Port.PortName = com;
                    Port.Open();
                    //  Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);
                    IsAlert = false;
                    return "ok";
                }
                else
                {
                    return "ok";
                }
            }
            catch (Exception e)
            {
                string exce = e.Message;
                return exce;

            }
        }

        /// <summary>
        /// 获取洗车机状态
        /// </summary>
        /// <param name="mes"></param>
        /// <returns></returns>
        public static bool CheckCar(string mes, int inputDC)
        {
            //发送数据       
            if (!Port.IsOpen)
            {
                Port.PortName = mes;
                Port.BaudRate = 9600;
                Port.Open();
                // Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);
            }

            byte[] strbyte = new byte[] { 0x00, 0x5A, 0x56, 0x07, 0x00, 0x00, 0x00, 0x00, 0xB7 };
            Port.Write(strbyte, 0, strbyte.Length);

            bool flag = true;
            DateTime delaystop = DateTime.Now.AddMilliseconds(300);
            while (delaystop > DateTime.Now)
            {
                if (Port.BytesToRead == 9)
                {
                  //  string getmes = Port.ReadExisting();
                   // byte[] byteArray = System.Text.Encoding.Default.GetBytes(getmes);
                  //  List<bool> x001 = WeiHelp.GetBitList(byteArray[7]).ToList<bool>();
                    for (int i = 0; i < 9; i++)
                    {
                        int nowinput=Port.ReadByte();
                        if (i == 7)
                        {
                            if (nowinput == 0xff)
                            {
                                flag = false;
                            }
                        }
                    }
                   // flag = !x001[inputDC - 1];
                   
                }

            }
            Port.Close();
            return flag;

        }

        /// <summary>
        /// 获取洗车机状态
        /// </summary>
        /// <param name="mes"></param>
        /// <returns></returns>
        public static bool GetStatus(string mes,int inputDC)
        {
            //发送数据       
            if (!Port.IsOpen)
            {
                Port.PortName = mes;
                Port.BaudRate = 9600;
                Port.Open();
                // Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);
            }

            byte[] strbyte = new byte[] { 0x00, 0x5A, 0x56 ,0x07 ,0x00, 0x00 ,0x00 ,0x00 ,0xB7 };
            Port.Write(strbyte, 0, strbyte.Length);
          
            bool flag = false;
            DateTime delaystop = DateTime.Now.AddMilliseconds(300);
            while (delaystop > DateTime.Now)
            {
                if (Port.BytesToRead == 9)
                {
                    for (int i = 0; i < 9; i++)
                    {
                        int nowinput = Port.ReadByte();
                        if (i == 7)
                        {
                            if (nowinput != 0xff)
                            {
                                flag = true ;
                            }
                        }
                    }

                }
                
            }
            Port.Close();
            return flag;

        }

        /// <summary>
        /// 复位报警
        /// </summary>
        /// <param name="mes"></param>
        /// <returns></returns>
        public static bool RestAlert(string mes)
        {
            //发送数据       
            if (!Port.IsOpen)
            {
                Port.PortName = mes;
                Port.BaudRate = 9600;
                Port.Open();
                // Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);
            }
            byte[] closeJDQ = new byte[] { 0x00, 0x5A, 0x56, 0x04, 0x00, 0x00, 0x00, 0x00, 0xB4 };
            Port.Write(closeJDQ, 0, closeJDQ.Length);
            // Port.Close();
            return true;
        }


        /// <summary>
        /// 发送开始洗车项目
        /// </summary>
        /// <param name="mes"></param>
        /// <returns></returns>
        public static bool sendOpen(string mes, int washType)
        {
            if (!IsAlert)
            {
                IsAlert = true;
                //发送数据       
                if (!Port.IsOpen)
                {
                    Port.PortName = mes;
                    Port.BaudRate = 9600;
                    Port.Open();
                    // Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);
                }

                byte[] strbyte1 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x01, 0x00, 0x00, 0x00, 0xB2 };
                byte[] strbyte2 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x02, 0x00, 0x00, 0x00, 0xB3 };
                byte[] strbyte3 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x03, 0x00, 0x00, 0x00, 0xB4 };
                byte[] strbyte4 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x04, 0x00, 0x00, 0x00, 0xB5 };
                byte[] strbyte5 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x05, 0x00, 0x00, 0x00, 0xB6 };
                byte[] strbyte6 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x06, 0x00, 0x00, 0x00, 0xB7 };
                byte[] strbyte7 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x07, 0x00, 0x00, 0x00, 0xB8 };
                byte[] strbyte8 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x08, 0x00, 0x00, 0x00, 0xB9 };
                byte[] strbyte = null;
                switch (washType)
                {
                    case 1:
                        strbyte = strbyte1;
                        break;
                    case 2:
                        strbyte = strbyte2;
                        break;
                    case 3:
                        strbyte = strbyte3;
                        break;
                    case 4:
                        strbyte = strbyte4;
                        break;
                    case 5:
                        strbyte = strbyte5;
                        break;
                    case 6:
                        strbyte = strbyte6;
                        break;
                    case 7:
                        strbyte = strbyte7;
                        break;
                    case 8:
                        strbyte = strbyte8;
                        break;
                }

                Port.Write(strbyte, 0, strbyte.Length);

                Thread.Sleep(AlertTime);
                byte[] closeJDQ = new byte[] { 0x00, 0x5A, 0x56, 0x04, 0x00, 0x00, 0x00, 0x00, 0xB4 };
                Port.Write(closeJDQ, 0, closeJDQ.Length);
                IsAlert = false;
            }
            return true;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="mes"></param>
        /// <returns></returns>
        public static string sendClose(string mes)
        {

            //发送数据       
            if (!Port.IsOpen)
            {
                Port.PortName = mes;
                Port.BaudRate = 9600;

                Port.Open();
                // Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);
            }
            // byte[] strbyte = new byte[] { 0x03, 0x03, 0x00, 0x00 };
            byte[] strbyte = new byte[] { 0x01, 0x05, 0x00, 0x04 };
            Port.Write(strbyte, 0, strbyte.Length);
            string retvaule = "失败";
            bool flag = false;
            DateTime delaystop = DateTime.Now.AddMilliseconds(300);
            while (delaystop > DateTime.Now)
            {
                if (Port.BytesToRead > 0)
                {
                    // flag = true;
                    // int ret = Port.BytesToRead;
                    //  if (ret == 4)
                    {
                        byte[] nowretur = new byte[4];
                        for (int i = 0; i < 4; i++)
                        {
                            nowretur[i] = (byte)Port.ReadByte();
                        }

                        for (int i = 0; i < 4; i++)
                        {
                            if (nowretur[i] != strbyte[i])
                            {
                                flag = true;

                            }
                            else
                            {
                                flag = false;
                                break;
                            }
                        }
                        break;

                    }
                }
            }
            if (flag)
            {
                retvaule = "成功";
            }
            Port.Close();
            return retvaule;

        }



        public static bool Close()
        {
            if(Port.IsOpen)
            Port.Close();

            return true;
        }
    }
}
